﻿Imports System.Text.RegularExpressions

Public Class RegistrationControl
    Private bFirstNameValidated As Boolean = False
    Private bLastNameValidated As Boolean = False
    Private bEmailValidated As Boolean = False
    Private bConfirmEmailValidated As Boolean = False
    Private bUsernameValidated As Boolean = False
    Private bPasswordValidated As Boolean = False
    Private bConfirmPasswordValidated As Boolean = False
    Private bAgeValidated As Boolean = False
    Private bCompanyTitleValidated As Boolean = False

    Private Sub RegistrationControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.cboForeignLanguage.SelectedIndex = 0
        Me.txtConfirmPassword.Text = Me.txtPassword.Text
    End Sub

    Private Sub txtFirstName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFirstName.Validating
        If txtFirstName.TextLength = 0 Then
            bFirstNameValidated = False
            FirstNameErrorProvider.SetError(txtFirstName, "First Name can't be empty")
        Else
            bFirstNameValidated = True
            FirstNameErrorProvider.Clear()
        End If
    End Sub

    Private Sub txtLastName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtLastName.Validating
        If txtLastName.TextLength = 0 Then
            bLastNameValidated = False
            LastNameErrorProvider.SetError(txtLastName, "Last Name can't be empty")
        Else
            bLastNameValidated = True
            LastNameErrorProvider.Clear()
        End If
    End Sub

    Private Sub txtEmail_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtEmail.Validating
        If txtEmail.TextLength = 0 Then
            bEmailValidated = False
            EmailErrorProvider.SetError(txtEmail, "E-mail can't be empty")
        ElseIf (ValidateEmail(txtEmail.Text)) Then
            ' When email is in proper format
            bEmailValidated = True
            EmailErrorProvider.Clear()
        Else
            EmailErrorProvider.SetError(txtEmail, "E-mail hasn't a proper format (example: username@somedomain.com)")
        End If
    End Sub

    Private Sub txtUsername_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtUsername.Validating
        If txtUsername.TextLength = 0 Then
            bUsernameValidated = False
            UsernameErrorProvider.SetError(txtUsername, "Username can't be empty")
        Else
            UsernameErrorProvider.Clear()

            If Me.ParentForm.Name = "Registration" Then
                Dim iCountUsersFound As Integer = Me.UserTableAdapter.isUser(txtUsername.Text)

                If (iCountUsersFound > 0) Then
                    bUsernameValidated = False
                    UsernameErrorProvider.SetError(txtUsername, "Username exists")
                Else
                    bUsernameValidated = True
                    UsernameErrorProvider.Clear()
                End If
            ElseIf Me.ParentForm.Name = "MainForm" Then
                bUsernameValidated = True
                UsernameErrorProvider.Clear()
            End If
        End If
    End Sub

    Private Sub txtPassword_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtPassword.Validating
        If txtPassword.TextLength = 0 Then
            bPasswordValidated = False
            PasswordErrorProvider.SetError(txtPassword, "Password can't be empty")
        Else
            bPasswordValidated = True
            PasswordErrorProvider.Clear()
        End If
        ValidatePasswords()
    End Sub

    Private Sub txtConfirmPassword_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtConfirmPassword.Validating
        ValidatePasswords()
    End Sub

    '===============================================================
    ' Validating Passwords
    '---------------------------------------------------------------
    'Purpose : Checks if passwords match and displays an error message 
    '          if they don't or clear the error message if they do.
    '
    ' Author : Chatzigeorgiou Konstantinos, 9 December 2012
    '===============================================================
    Private Sub ValidatePasswords()
        If txtConfirmPassword.Text <> txtPassword.Text Then
            bConfirmPasswordValidated = False
            ConfirmPasswordErrorProvider.SetError(txtConfirmPassword, "Password don't match")
        Else
            bConfirmPasswordValidated = True
            ConfirmPasswordErrorProvider.Clear()
        End If
    End Sub

    '===============================================================
    ' Validating Emails
    '---------------------------------------------------------------
    'Purpose : Checks if emails match and displays an error message 
    '          if they don't or clear the error message if they do.
    '
    ' Author : Chatzigeorgiou Konstantinos, 6 January 2013
    '===============================================================
    Private Sub ValidateEmails()
        If txtConfirmEmail.Text <> txtEmail.Text Then
            bConfirmEmailValidated = False
            ConfirmEmailErrorProvider.SetError(txtConfirmEmail, "Emails don't match")
        Else
            bConfirmEmailValidated = True
            ConfirmEmailErrorProvider.Clear()
        End If
    End Sub

    Private Sub chkEmployee_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEmployee.CheckedChanged
        grbEmployee.Visible = If(chkEmployee.Checked, True, False)
    End Sub

    Private Sub chkEmployer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEmployer.CheckedChanged
        grbEmployer.Visible = If(chkEmployer.Checked, True, False)
    End Sub

    Private Sub txtAge_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtAge.Validating
        If txtAge.TextLength = 0 Then
            bAgeValidated = False
            AgeErrorProvider.SetError(txtAge, "Age can't be empty")
        ElseIf Convert.ToDouble(txtAge.Text) < 15 Then
            bAgeValidated = False
            AgeErrorProvider.SetError(txtAge, "Age can't be less than 15")
        Else
            bAgeValidated = True
            AgeErrorProvider.Clear()
        End If
    End Sub

    Private Sub txtAge_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAge.KeyPress
        If Not IsNumeric(e.KeyChar) AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtCompanyTitle_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCompanyTitle.Validating
        If txtCompanyTitle.TextLength = 0 Then
            bCompanyTitleValidated = False
            CompanyTitleErrorProvider.SetError(txtCompanyTitle, "Company Title can't be empty")
        Else
            bCompanyTitleValidated = True
            CompanyTitleErrorProvider.Clear()
        End If
    End Sub

    '===============================================================
    ' Validating User's Fields
    '---------------------------------------------------------------
    'Purpose : Checks if user's fields are valid and returns true 
    '          if they are, false if they aren't.
    '
    ' Author : Chatzigeorgiou Konstantinos, 9 December 2012
    '===============================================================
    Private Function ValidateUserFields() As Boolean
        Return bFirstNameValidated And bLastNameValidated And bEmailValidated And bConfirmEmailValidated And bUsernameValidated And bPasswordValidated And bConfirmPasswordValidated
    End Function

    '===============================================================
    ' Validating Employee's Fields
    '---------------------------------------------------------------
    'Purpose : Checks if employee's fields are valid and returns true 
    '          if they are, false if they aren't.
    '
    ' Author : Chatzigeorgiou Konstantinos, 9 December 2012
    '===============================================================
    Private Function ValidateEmployeeFields() As Boolean
        Return bAgeValidated
    End Function

    '===============================================================
    ' Validating Employer's Fields
    '---------------------------------------------------------------
    'Purpose : Checks if employer's fields are valid and returns true 
    '          if they are, false if they aren't.
    '
    ' Author : Chatzigeorgiou Konstantinos, 9 December 2012
    '===============================================================
    Private Function ValidateEmployerFields() As Boolean
        Return bCompanyTitleValidated
    End Function

    '===============================================================
    ' Validating Fields
    '---------------------------------------------------------------
    'Purpose : Checks if fields are valid and returns true if they 
    '          are, false if they aren't.
    '
    ' Author : Chatzigeorgiou Konstantinos, 9 December 2012
    '===============================================================
    Public Function ValidateFields() As Boolean
        If Not Me.ValidateUserFields() Or (Me.chkEmployee.Checked And Not Me.ValidateEmployeeFields()) Or (Me.chkEmployer.Checked And Not Me.ValidateEmployerFields()) Then
            Return False
        Else
            Return True
        End If
    End Function

    '===============================================================
    ' Validating Email format
    '---------------------------------------------------------------
    ' Purpose : Checks if email format has the general known format
    '
    ' Author : John Paraskevopoulos, 6 January 2012
    '===============================================================
    Private Function ValidateEmail(ByVal emailAddress As String) As Boolean
        Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
        Dim emailAddressMatch As Match = Regex.Match(emailAddress, pattern)
        If emailAddressMatch.Success Then
            ValidateEmail = True
        Else
            ValidateEmail = False
        End If
    End Function

    Private Sub txtConfirmEmail_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtConfirmEmail.Validating
        ValidateEmails()
    End Sub
End Class
